package bman.game.battletank;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.BoxLayout;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bman.game.battletank.BattleTank;
import bman.game.engine.Game;
import bman.game.engine.GameConsole;
import bman.game.engine.GraphicsEngine;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GamePanel extends JPanel implements ActionListener, KeyListener
{
	static int port = 100;
	private JButton startButton;

	private JPanel jPanel1;

	private JTextField address;

	private JButton connect;
	
	GameConsole console = new GameConsole();

	public GamePanel()
	{
		console.loadGame(new BattleTank());
		initGUI();
	}

	private void initGUI()
	{
		try
		{
			GraphicsEngine g = console.getGraphicsEngine();
			add(g);
			setSize(g.getSize());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		
		String cmd = e.getActionCommand();
	    System.out.println("Source: "+cmd);
	     
	    try
		{
			if(cmd.contains("Connect"))
			{
		    	String address = (String)JOptionPane.showInputDialog(
	                       this.getParent(), 
	                       "Connect to a Battle Tank Server....\n" +
	                       "Enter the IP address and port: \n " +
	                       "For example 127.0.0.1:777",
	                       "127.0.0.1:777");
		    	System.out.println(address);
		    	  
				console.connect(address);
				
				join();
			}
			else if("Start".equalsIgnoreCase(cmd))
			{
				console.loadGame(new BattleTank());
				console.performAction(console.START);
				
				join();
			}
			
		}
		catch (Exception e1)
		{
			e1.printStackTrace();
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e1.printStackTrace(pw);
			JOptionPane.showMessageDialog(this.getParent(), 
					"A fatal error has occurred. Program will now terminate." +
					"\n\nError is: "+e1.getMessage()+"\n\n" +
				    "Stack Trace:\n\n"+sw.toString());
			System.exit(1);
			
		}

	}
	
	private void join()
	{
		String[] options = {"Green","Red","Blue","Yellow","Pink"};
		String color = (String)JOptionPane.showInputDialog(
		                    this.getParent(),
		                    "Please select tank color",
		                    "Select Tank Color",
		                    JOptionPane.PLAIN_MESSAGE,
		                    null,
		                    options,
		                    options[0]);
		System.out.println("Start. Color selected is "+color);
		console.performAction(BattleTank.JOIN+" COLOR:"+color);
	}
	
	public void keyPressed(KeyEvent ke)
	{
		System.out.println("KeyPressed: "+ke.getKeyCode());
		console.getController().perform(ke);
	}
	
	public void keyReleased(KeyEvent ke)
	{
	}
	
	public void keyTyped(KeyEvent ke)
	{
	}
}